<?php

/**
 * ECTouch Open Source Project
 * ============================================================================
 * Copyright (c) 2012-2014 http://ectouch.cn All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：CommonControoller.class.php
 * ----------------------------------------------------------------------------
 * 功能描述：公共控制器
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.ectouch.cn/docs/license.txt )
 * ----------------------------------------------------------------------------
 */

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class CommonController extends BaseController
{

    protected static $user = NULL;

    protected static $sess = NULL;

    protected static $view = NULL;

    public function __construct()
    {
        parent::__construct();

         $this->ecshop_init();
        // 微信oauth处理
        if(class_exists('WechatController')){
            if (method_exists('WechatController', 'do_oauth')) {
                call_user_func(array('WechatController', 'do_oauth'));
            }
        }
        /* 语言包 */
        $this->assign('lang', L());
        /* 页面标题 */
        $page_info = get_page_title();
        self::$view->assign('wechat_gzh', '中服运动');
        self::$view->assign('page_title', $page_info['title']);
        self::$view->assign('meta_keywords', C('shop_keywords'));
        self::$view->assign('meta_description', C('shop_desc'));
        $url=$_SERVER['HTTP_REFERER'];
        self::$view->assign('url',  $url);
        
        //判断是否在店铺中购买
        if($_GET['mshopNo'])
        {
            if($_SESSION['user_mshopNo'])
            {
                $_SESSION['mshopNo']=$_SESSION['user_mshopNo'];
            }
            else
            {
                 //  $whereNo['mshop_id']=$_GET['mshopNo'];
                 //  $row = $this->model->table('users')
                 //        ->field('user_id')
                 //        ->where($whereNo)
                 //        ->find();
                 // if($row)
                 // {
                    $_SESSION['mshopNo']=$_GET['mshopNo'];
                    
                 // }
            }
                
        
        }


        //会员已有店铺的优先级高
        if($_SESSION['user_id']&&!$_SESSION['user_mshopNo'])
        {
              $whereNo_u['user_id']=$_SESSION['user_id'];
              $row_u = $this->model->table('users')
                    ->field('mshop_id')
                    ->where($whereNo_u)
                    ->find();
             if($row_u['mshop_id'])
             {
                $_SESSION['user_mshopNo']=$row_u['mshop_id'];//用户店铺ID
                $_SESSION['mshopNo']=$row_u['mshop_id'];
             }
             
        }
        elseif($_SESSION['openid']&&!$_SESSION['mshopNo'])
        {
               $where_No['uopenid']= $_SESSION['openid'];
                $user_No= $this->model->table('mshop_user_uopenid')
                ->field('mshop_id')
                ->where($where_No)
                ->find();
                if($user_No['mshop_id'])
                {
                   $_SESSION['mshopNo']=$user_No['mshop_id']; 
                }
                
        }

		

        $category = model('CategoryBase')->get_categories_tree();

        self::$view->assign('category_cats', $category);
        //微信登录
        if($_SESSION['openid']&&!$_SESSION['user_id'])
        {           
            $_SESSION['user_mshopNo']='';//特殊情况，更换微信登录
            $status=model('Users')->check_wxreg($_SESSION['openid']);
            $r_url=$_GET['redirect_uri'];
            if($r_url)
            header('Location: '.$r_url);
            else
            $this->redirect(url('index'));
        }

        //判断是否关注公众号
        if($_SESSION['openid']&&!$_SESSION['is_subscribe'])
        {

            $is_subscribe=model('Wechat')->check_subscribe();
            $_SESSION['is_subscribe']=$is_subscribe;
            $no_subscribe=$is_subscribe?false:true;
            self::$view->assign('is_subscribe', $no_subscribe);
        }
        /* 模板赋值 */
        assign_template();        
    }
     //$upload_dir上传的目录名
    protected function UploadImage($key = '', $upload_dir = 'images', $thumb = false, $width = 220, $height = 220) {
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 1024 * 1024 * 2; //最大2M
        //设置上传文件类型
        $upload->allowExts = explode(',', 'jpg,jpeg,gif,png,bmp,mp3,amr,mp4');
        //生成缩略图
        $upload->thumb = $thumb;
        //缩略图大小
        $upload->thumbMaxWidth = $width;
        $upload->thumbMaxHeight = $height;

        //设置附件上传目录
        $upload->savePath = './images/' . $upload_dir . "/";

        if (!$upload->upload($key)) {
            //捕获上传异常
            return array('error' => 1, 'message' => $upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            return array('error' => 0, 'message' => $upload->getUploadFileInfo());
        }
    }

    static function user()
    {
        return self::$user;
    }

    static function sess()
    {
        return self::$sess;
    }

    static function view()
    {
        return self::$view;
    }

    protected function assign($tpl_var, $value = '')
    {
        self::$view->assign($tpl_var, $value);
    }

    protected function display($tpl = '', $cache_id = '', $return = false)
    {
        self::$view->display($tpl, $cache_id);
    }

    protected function ecshop_init()
    {
        header('Cache-control: private');
        header('Content-type: text/html; charset=utf-8');
        
        $shop_closed = C('shop_closed');
        if (! empty($shop_closed)) {
            $close_comment = C('close_comment');
            $close_comment = empty($close_comment) ? 'closed.':$close_comment;
            exit($close_comment);
        }
        //NULL
        // 初始化session
        self::$sess = new EcsSession(self::$db, self::$ecs->table('sessions'), self::$ecs->table('sessions_data'), 'ecsid');
        define('SESS_ID', self::$sess->get_session_id());
        
        // 创建 Smarty 对象
        self::$view = new EcsTemplate();
        self::$view->cache_lifetime = C('cache_time');
        self::$view->template_dir = ROOT_PATH . 'themes/' . C('template');
        self::$view->cache_dir = ROOT_PATH . 'data/cache/caches';
        self::$view->compile_dir = ROOT_PATH . 'data/cache/compiled';
        
        if ((DEBUG_MODE & 2) == 2) {
            self::$view->direct_output = true;
            self::$view->force_compile = true;
        } else {
            self::$view->direct_output = false;
            self::$view->force_compile = false;
        }
        self::$view->caching = true;
        
        // 会员信息
        self::$user = init_users();
        if (empty($_SESSION['user_id'])) {
            if (self::$user->get_cookie()) {
                // 如果会员已经登录并且还没有获得会员的帐户余额、积分以及优惠券
                if ($_SESSION['user_id'] > 0 && ! isset($_SESSION['user_money'])) {
                    model('Users')->update_user_info();
                }
            } else {
                $_SESSION['user_id'] = 0;
                $_SESSION['user_name'] = '';
                $_SESSION['email'] = '';
                $_SESSION['user_rank'] = 0;
                $_SESSION['discount'] = 1.00;
            }
        }
        
        // 判断是否支持gzip模式
        if (gzip_enabled()) {
            ob_start('ob_gzhandler');
        }
        
        // 设置推荐会员
        if (isset($_GET['u'])) {
            set_affiliate();
        }
        
        // session不存在，检查cookie
        if (! empty($_COOKIE['ECS']['user_id']) && ! empty($_COOKIE['ECS']['password'])) {
            // 找到cookie,验证信息
            $where['user_id'] = $_COOKIE['ECS']['user_id'];
            $where['password'] = $_COOKIE['ECS']['password'];
            $row = $this->model->table('users')
                ->field('user_id, user_name, password')
                ->where($where)
                ->find();
            if ($row) {
                $_SESSION['user_id'] = $row['user_id'];
                $_SESSION['user_name'] = $row['user_name'];
                model('Users')->update_user_info();
            } else {
                // 没有找到这个记录
                $time = time() - 3600;
                setcookie("ECS[user_id]", '', $time, '/');
                setcookie("ECS[password]", '', $time, '/');
            }
        }
        $ua = strtolower($_SERVER['HTTP_USER_AGENT']);

        if( preg_match('/micromessenger/', $ua)){

            if(!isset($_SESSION["openid"])||empty($_SESSION["openid"])){//openid为空

                include_once (ROOT_PATH . 'plugins/payment/wxpay.php');

                $payObj = new wxpay();

                if(isset($_GET['state']) && $_GET['state']=="getOpenid"){

                    $code=$_GET["code"];

                    //$pay_online = $pay_obj->get_code($order, unserialize_config($payment ['pay_config']));

                    //todo

                    $payObj->getOpenidByCode($code);

                }else{

                    $p["state"]="getOpenid";

                    // $p["redirect_uri"]=__URL__;
                    $p["redirect_uri"]='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

                    $payObj->redirtUrlForOpenid($p);

                }

            }

        } 
        // search 关键词
        $search_keywords = C('search_keywords');
        if (!empty($search_keywords) && is_string($search_keywords)) {
            $keywords = explode(',', $search_keywords);
            $this->assign('hot_search_keywords', $keywords);
        }
        // 模板替换
        defined('__TPL__') or define('__TPL__', __ROOT__ . '/themes/' . C('template'));
        $stylename = C('stylename');
        if (! empty($stylename)) {
            $this->assign('ectouch_css_path', __ROOT__ . '/themes/' . C('template') . '/css/ectouch_' . C('stylename') . '.css');
        } else {
            $this->assign('ectouch_css_path', __ROOT__ . '/themes/' . C('template') . '/css/ectouch.css');
        }
    }
}

class_alias('CommonController', 'ECTouch');
